package com.wd.live.platform;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class QuanminRepoPageProcessor  extends AbstractProcessor {

	// 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
	 private Pipeline pipeline;
	  public QuanminRepoPageProcessor(Pipeline pipeline){
		  this.pipeline = pipeline;
	  }
	  private QuanminRepoPageProcessor(){
	  }
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void process(Page page) {
		Map map = (Map) JSON.parse(page.getRawText());
		//map = (Map) map.get("data");
		List<JSONObject> listMap = (List<JSONObject>) map.get("data");
		List<String[]> result = new LinkedList<String[]>();
		for (JSONObject obj : listMap) {
			String[] array = new String[4];
			array[0] = obj.get("category_name").toString();
			array[1] = obj.get("nick").toString();
			array[2] = obj.get("view").toString();
			
			
			array[3] = "全民";
			result.add(array);
		}
		
		page.putField("listDate", result);
	}

	public Site getSite() {

		return site;
	}

	public void execute() {
		Spider.create(new QuanminRepoPageProcessor()).addUrl("http://www.quanmin.tv/json/play/list.json").addPipeline(pipeline)
				// 开启5个线程抓取
				.thread(5)
				// 启动爬虫
				.run();
	}
}